#pragma once

#include "spectrumdlg.h"
#include "scalerangedlg.h"
#include "imagesizedlg.h"
#include "freqbanddlg.h"

class CFFTMainFrame: public CFrameWindowImpl<CFFTMainFrame>, public CUpdateUI<CFFTMainFrame>,
		public CMessageFilter, public CIdleHandler
{
	//friend class FFTMainFrame;

public:
	DECLARE_FRAME_WND_CLASS(_T("FFT Window Class"), IDR_FFTMAINFRAME)

	CFFTGraphView m_view;

	virtual BOOL PreTranslateMessage(MSG* pMsg)
	{
		if(CFrameWindowImpl<CFFTMainFrame>::PreTranslateMessage(pMsg))
			return TRUE;

		return m_view.PreTranslateMessage(pMsg);
	}

	virtual BOOL OnIdle()
	{
		UIUpdateToolBar();
		return FALSE;
	}

	BEGIN_UPDATE_UI_MAP(CFFTMainFrame)
		UPDATE_ELEMENT(ID_VIEW_TOOLBAR, UPDUI_MENUPOPUP)
		UPDATE_ELEMENT(ID_VIEW_STATUS_BAR, UPDUI_MENUPOPUP)
	END_UPDATE_UI_MAP()

	BEGIN_MSG_MAP(CFFTMainFrame)
		MESSAGE_HANDLER(WM_CLOSE, OnClose)
		MESSAGE_HANDLER(WM_DESTROY, OnDestroy)
		//MESSAGE_HANDLER(WM_PAINT, OnPaint)

		MESSAGE_HANDLER(WM_CREATE, OnCreate)

		//toolbar
		COMMAND_ID_HANDLER(ID_FFTSWITCHVIEW,OnFFT_SWITCHVIEW)

		//menu
		COMMAND_ID_HANDLER(ID_FILE_CLOSEFFT,OnMenuClose)
		COMMAND_ID_HANDLER(ID_FILE_COPY,OnMenuCopy)
		COMMAND_ID_HANDLER(ID_SETTINGS_SPECTRUM, OnSpeDlg)
		COMMAND_ID_HANDLER(ID_SETTINGS_SETRANGE, OnRangeDlg)
		COMMAND_ID_HANDLER(ID_SETTINGS_BITMAPSIZE, OnBmpSizeDlg)
		COMMAND_ID_HANDLER(ID_SETTINGS_FREQBAND, OnFreqBandDlg)

		CHAIN_MSG_MAP(CUpdateUI<CFFTMainFrame>)
		CHAIN_MSG_MAP(CFrameWindowImpl<CFFTMainFrame>)


	END_MSG_MAP()

	LRESULT OnCreate(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
	{

		CreateSimpleToolBar();

		CreateSimpleStatusBar();

		m_hWndClient = m_view.Create(m_hWnd, rcDefault, NULL, WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, WS_EX_CLIENTEDGE);

		UIAddToolBar(m_hWndToolBar);
		UISetCheck(ID_VIEW_TOOLBAR, 1);
		UISetCheck(ID_VIEW_STATUS_BAR, 1);

		// register object for message filtering and idle updates
		CMessageLoop* pLoop = _Module.GetMessageLoop();
		ATLASSERT(pLoop != NULL);
		pLoop->AddMessageFilter(this);
		pLoop->AddIdleHandler(this);

		return 0;
	}

	LRESULT OnClose(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
	{
		DestroyWindow();
		return 0;
	}

	LRESULT OnDestroy(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
	{
		PostQuitMessage(0);
		return 0;
	}

	LRESULT OnCreate(LPCREATESTRUCT pcs){
		UINT nResourceID = GetWndClassInfo().m_uCommonResourceID;

		m_hWndToolBar = CreateSimpleToolBarCtrl(m_hWnd, 
			nResourceID, TRUE, ATL_SIMPLE_TOOLBAR_STYLE, ATL_IDW_TOOLBAR);

		SetWindowPos(HWND_NOTOPMOST, 0, 0 ,FFT_WND_WIDTH ,FFT_WND_HEIGHT, SWP_NOMOVE);
		SetWindowLong( GWL_STYLE,WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU); 
		SetMsgHandled(false);
		return 0;
	}

/*	LRESULT OnPaint(HDC hdc)
	{
		PAINTSTRUCT ps;
		hdc = BeginPaint(&ps);
		OnDraw(hdc);
		EndPaint(&ps);
		return 1;
	}*/
	LRESULT OnFFT_SWITCHVIEW(WORD,WORD,HWND, BOOL& bHandled)
    {
		if(m_view.selectedChannelEID ==-1)
		{
			MessageBox(_T("Select a channel first"));
			return 1;
		}
		if(m_view.selectedChannelEID!=-1 && m_view.viewType == false)
			m_view.viewType = true;
		else if( m_view.viewType == true)
			m_view.viewType = false;
		//UISetText ( 1, _T("Running") );
		m_view.Invalidate();
		m_view.UpdateWindow();
		return 1;
	}

	LRESULT OnMenuClose(WORD,WORD,HWND, BOOL& bHandled)
    {
		DestroyWindow();
		return 1;
	}

	LRESULT OnMenuCopy(WORD,WORD,HWND, BOOL& bHandled)
    {
		//DestroyWindow();
		Bitmap bmp(Config::bmpWidth, Config::bmpHeight);
		if(m_view.selectedChannelEID!=-1){
			m_view.DrawBitmap(  m_view.selectedChannelEID, bmp,Config::bmpWidth,Config::bmpHeight);
				copy2clipboard(&bmp, Color(0,0,0));
		}else
			MessageBox(_T("Please select a channel."));
		return 1;
	}

	LRESULT OnSpeDlg(WORD,WORD,HWND, BOOL& bHandled)
    {
		CSpectrumDlg sd;
		if(sd.DoModal() == IDOK)
			m_view.RedrawNow();
		return 1;
		
	}
	LRESULT OnRangeDlg(WORD,WORD,HWND, BOOL& bHandled)
    {
		CScaleRangeDlg sd;
		sd.SetRange(m_view.d_min, m_view.d_max);
		sd.SetRangeType(m_view.r_range);

		sd.SetUnit(m_view.r_unit);
		if(sd.DoModal() == IDOK){
			m_view.r_range = sd.GetRangeType();
			m_view.r_unit = sd.GetUnit();
			sd.GetRange( m_view.d_min, m_view.d_max);

			m_view.RedrawNow();
		}
		return 1;
	}
	
	LRESULT OnBmpSizeDlg(WORD,WORD,HWND, BOOL& bHandled)
    {
		CImageSizeDlg isd;
		isd.DoModal();
		return 1;
		
	}
	LRESULT OnFreqBandDlg(WORD,WORD,HWND, BOOL& bHandled)
    {
		CFreqBandDlg fbd;
		fbd.DoModal();
		return 1;
		
	}
	
public:
	//void SetSource( FFTCalculator *source){m_view.calc = source;}


};
